package com.moleq.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.PosLineItem;

public class PosLineItemService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;
	
	public PosLineItemService(){}
	
	public PosLineItemService(Context context){
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public void DeleteAll() {
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		DBHelper.execSQL("DELETE FROM "+ DatabaseSchema.PosLineItem.SCHEMA_NAME);
	}
	
	public void DeleteByNo(long no) {
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		DBHelper.execSQL("DELETE FROM "+ DatabaseSchema.PosLineItem.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PosLineItem.PL_NO +" = ?",
				new Object[] { no });
	}
	
	public void DeleteAndUpdateNo(long no) {
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		DBHelper.execSQL("DELETE FROM "+ DatabaseSchema.PosLineItem.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PosLineItem.PL_NO +" <= ?",
				new Object[] { no });
		DBHelper.execSQL("Update " + DatabaseSchema.PosLineItem.SCHEMA_NAME + " set pl_no = pl_no - "+ no);
	}
	
	public List<PosLineItem> FindAll(){
		List<PosLineItem> obj = new ArrayList<PosLineItem>();
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = DBHelper.rawQuery("select * from " + DatabaseSchema.PosLineItem.SCHEMA_NAME + " order by pl_no", null);
		if (cursor == null) {
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
//			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			long no = cursor.getLong(cursor.getColumnIndex(DatabaseSchema.PosLineItem.PL_NO));
			String upc = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineItem.PL_UPC));
			int qty = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineItem.PL_QTY));
			double weight = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosLineItem.PL_WEIGHT));
			double price = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosLineItem.PL_PRICE));
			String type = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineItem.PL_TYPE));
			obj.add(new PosLineItem(no, upc, qty, weight, price,type));
		}
		cursor.close();
		return obj;
	}
	
	public void Insert(PosLineItem obj){
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		DBHelper.execSQL(
				"INSERT INTO " + DatabaseSchema.PosLineItem.SCHEMA_NAME + " VALUES(?,?,?,?,?,?)",
				new Object[] { obj.getNo(), obj.getUpc(), obj.getQty(), obj.getWeight(), obj.getPrice() ,obj.getType() });
	}
	public void Update(PosLineItem obj){
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		DBHelper.execSQL("UPDATE " + DatabaseSchema.PosLineItem.SCHEMA_NAME + 
				" set pl_upc = ?,pl_qty = ?,pl_weight = ?,pl_price = ?,pl_type = ? where pl_no = ?",
				new Object[] { obj.getUpc(), obj.getQty(), obj.getWeight(), obj.getPrice() ,obj.getType() ,obj.getNo()});
	}
	
	public void UpdatePriceByNo(long no,double price){
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "UPDATE " + DatabaseSchema.PosLineItem.SCHEMA_NAME + " set pl_price = " + price +" where pl_no = "+no;
		DBHelper.execSQL(sql);
	}
	
	public long getMaxNo(){
		long no = 0;
//		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = DBHelper.rawQuery("select max(pl_no) from " + DatabaseSchema.PosLineItem.SCHEMA_NAME, null);
		if (cursor.moveToNext()) {
			no = cursor.getLong(0);
		}
		cursor.close();
		return no;
	}
//	public void ShowMessage(String text, int type, int icon) {
//		mMessageDialog = new MessageDialog(context);
//		mMessageDialog.setStyle(type);
//		mMessageDialog.setMessageIcon(icon);
//		mMessageDialog.setMessage(text);
//		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
//		mMessageDialog.show();
//	}
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
